package com.zhuge_new.Base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.zhuge_new.R;
import com.zhuge_new.Untils.ActivityStack;
import com.zhuge_new.Untils.StatusBarUtil;

/**
 * Created by Administrator on 2018/10/25.
 */

public abstract class BaseActivity extends AppCompatActivity implements BaseView, View.OnClickListener {

    private TextView tvTitle;
    private ImageView ivBack;
    private ImageView ivRight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        Activity baseContext=this;
        ActivityStack.getScreenManager().pushActivity(this);
        initdefalt();
    }

    protected void initdefalt(){
        StatusBarUtil.setStatusBarLightMode(this);
    }

    protected void initToolbar(int colorId){
        StatusBarUtil.setStatusBarColor(this,colorId);
    }

    protected void inittitle(String msg) {
        tvTitle = findViewById(R.id.tv_nav_title);
        ivBack = findViewById(R.id.iv_nav_back);
        ivRight = findViewById(R.id.iv_nav_right);
        tvTitle.setText(msg);
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popActivity();
                break;
        }
    }


    @Override
    public Context getContext() {
        return BaseActivity.this;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            // 获得当前得到焦点的View，一般情况下就是EditText（特殊情况就是轨迹求或者实体案件会移动焦点）
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                hideSoftInput(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时没必要隐藏
     */
    private boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            return !(event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom);
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditView上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 多种隐藏软件盘方法的其中一种
     */
    private void hideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (im == null) return;
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    protected void onDestroy() {
        OkGo.getInstance().cancelTag(this);
        super.onDestroy();
    }

}
